@extends('frontend/layouts/bootstrap')

@section('content')
<h2>Машины жагсаалт - Нэхэмжлэл</h2>

{{-- FILTER --}}
<form class="form-inline" style="margin: 0;" action="{{ route('car_balance.bill.create.step1_filter') }}" method="post">
    <table class="table table-bordered">
        <thead style="background-color: #f0f0f0;">
        <tr>
            <th>車体番号</th>
            <th>日付</th>
            <th>Customer</th>
            <th>Action</th>
        </tr>
        </thead>

        <tbody>
        <tr>
            <td>
                <input placeholder="車体番号" type="text" name="chassis_no" class="input-small" value="{{ $filters['chassis_no'] ? $filters['chassis_no'] : '' }}"/>
            </td>
            <td>
                <input class="input-small" placeholder="日付" type="text" name="purchase_date" id="daterange" value="{{ count($filters['purchase_date']) > 1 ? $filters['purchase_date'][0] .' - '.$filters['purchase_date'][1] : '' }}"/>
            </td>
            <td>
                {{ Form::select('customer_id', $customer_list, $filters['customer_id'], array('class' => 'input-small')) }}
            </td>
            <td>
                {{ Form::token() }}
                <button type="submit" class="btn">Apply Filter</button>
                <a href="{{ route('car_balance.bill.create.step1') }}?reset=1" >Reset</a>
            </td>
        </tr>
        </tbody>
    </table>
</form>

@if ($cars->count())
<div>{{ $cars->count() }} cars results.</div>
<table class="table table-bordered table-condensed table-hover">
    <thead style="background-color: #f0f0f0;white-space: nowrap;">
    <tr>
        <th title="Action" style="width: 40px;">
            <input id="all_bill_flag" placeholder="チェック" type="checkbox" class="input-small" style="float: left; margin-top: 10px;" onclick="setAllBillFlag(this.checked)" name="all_bill_flag" {{ $filters['customer_id'] == '0' ? 'disabled' : '' }}/><label style="float: left; padding-left: 5px; padding-top: 5px;" for="all_bill_flag">All</label>
        </th>
        <th title="Date">日付</th>
        <th title="Customer">Customer</th>
        <th title="Auction">オーク会場</th>
        <th title="Auction no">出品番号</th>
        <th title="Car name">車種</th>
        <th title="Car year">年式</th>
        <th title="Chassis number">車体番号</th>
        <th style="text-align: center;" title="Bill price">購入額</th>
        <th style="text-align: center;" title="Vat percent">消費税</th>
        <th style="text-align: center;" title="Recycle amount">リサイクル料</th>
        <th style="text-align: center;" title="Auction commission">落札料</th>
        <th style="text-align: center;" title="Transport fee">Transport</th>
        <th style="text-align: center;" title="Vanning">Vanning</th>
        <th style="text-align: center;" title="Others">Others</th>
        <th style="text-align: center;" title="Commission">Commission</th>
        <th style="text-align: center;" title="Bill total amount">合計</th>
    </tr>
    </thead>
    <tbody style="font-size: 11px;">
    @foreach($cars as $car)
    <tr>
        <td>
            <input id="bill_{{ $car->id }}" placeholder="チェック" type="checkbox" class="bill_chbox input-small" style="margin-bottom: 5px; margin-right: 5px;" name="bill_flag" {{ $filters['customer_id'] == '0' ? 'disabled' : '' }}/>
        </td>
        <td>{{ $car->purchase_date }}</td>
        <td>{{ $car->customer ? link_to_route('car_balance.customer.show', $car->customer->name, $car->customer->id, array()) : 'NULL' }}</td>
        <td>{{ $car->auction_name }}</td>
        <td>{{ $car->lot_no }}</td>
        <td>{{ $car->car_maker }} {{ $car->car_name }}</td>
        <td>{{ $car->car_year }}</td>
        <td>{{ $car->chasis_no }}</td>
        <td style="text-align: right;">{{ $car->bill_price ? number_format($car->bill_price, 0) : 0; }}</td>
        <td style="text-align: right;">{{ number_format(($car->bill_price*$car->bill_vat)/100, 0); }}</td>
        <td style="text-align: right;">{{ $car->bill_recycle ? number_format($car->bill_recycle, 0) : 0; }}</td>
        <td style="text-align: right;">{{ $car->bill_rakusatu ? number_format($car->bill_rakusatu, 0) : 0; }}</td>
        <td style="text-align: right;">{{ $car->bill_transport_fee ? number_format($car->bill_transport_fee, 0) : 0; }}</td>
        <td style="text-align: right;">{{ $car->bill_vanning ? number_format($car->bill_vanning, 0) : 0; }}</td>
        <td style="text-align: right;">{{ $car->bill_others ? number_format($car->bill_others, 0) : 0; }}</td>
        <td style="text-align: right;">{{ $car->bill_commission ? number_format($car->bill_commission, 0) : 0; }}</td>
        <td style="text-align: right;">{{ $car->bill_total_amount ? number_format($car->bill_total_amount, 0) : 0; }}</td>
    </tr>
    @endforeach
    </tbody>
</table>

<!--<button class="btn btn-large btn-success" onclick="billCreateStep2()" {{ $filters['customer_id'] == '0' ? 'disabled' : '' }}><i class="icon-plus icon-white"></i> Үргэлжлүүлэх</button>-->
@endif

@if ($penalties1->count())
<div>Tulbur udaasan {{ $penalties1->count() }} penalty results.</div>
<table class="table table-bordered table-condensed table-hover">
    <thead style="background-color: #f0f0f0;white-space: nowrap;">
    <tr>
        <th title="Action" style="width: 40px;">
            <input id="all_penalty_flag1" placeholder="チェック" type="checkbox" class="input-small" style="float: left; margin-top: 10px;" onclick="setAllPenaltyFlag1(this.checked)" name="all_penalty_flag1" {{ $filters['customer_id'] == '0' ? 'disabled' : '' }}/><label style="float: left; padding-left: 5px; padding-top: 5px;" for="all_penalty_flag1">All</label>
        </th>
        <th title="Penalty date">Penalty date</th>
        <th title="Chassis number">車体番号</th>
        <th style="text-align: center;" title="Penalty amount">Penalty amount</th>
    </tr>
    </thead>
    <tbody style="font-size: 11px;">
    @foreach($penalties1 as $penalty)
    <tr>
        <td>
            <input id="penalty1_{{ $penalty->id }}" placeholder="チェック" type="checkbox" class="penalty_chbox1 input-small" style="margin-bottom: 5px; margin-right: 5px;" name="penalty_flag1" {{ $filters['customer_id'] == '0' ? 'disabled' : '' }}/>
        </td>
        <td>{{ $penalty->penalty_date }}</td>
        <td>{{ $penalty->chasis_no }}</td>
        <td style="text-align: right;">{{ $penalty->penalty_amount ? number_format($penalty->penalty_amount, 0) : 0; }}</td>
    </tr>
    @endforeach
    </tbody>
</table>
@endif

@if ($penalties2->count())
<div>Auction-oos irsen {{ $penalties2->count() }} penalty results.</div>
<table class="table table-bordered table-condensed table-hover">
    <thead style="background-color: #f0f0f0;white-space: nowrap;">
    <tr>
        <th title="Action" style="width: 40px;">
            <input id="all_penalty_flag2" placeholder="チェック" type="checkbox" class="input-small" style="float: left; margin-top: 10px;" onclick="setAllPenaltyFlag2(this.checked)" name="all_penalty_flag2" {{ $filters['customer_id'] == '0' ? 'disabled' : '' }}/><label style="float: left; padding-left: 5px; padding-top: 5px;" for="all_penalty_flag2">All</label>
        </th>
        <th title="Penalty date">Penalty date</th>
        <th title="Chassis number">車体番号</th>
        <th style="text-align: center;" title="Penalty amount">Penalty amount</th>
    </tr>
    </thead>
    <tbody style="font-size: 11px;">
    @foreach($penalties2 as $penalty)
    <tr>
        <td>
            <input id="penalty2_{{ $penalty->id }}" placeholder="チェック" type="checkbox" class="penalty_chbox2 input-small" style="margin-bottom: 5px; margin-right: 5px;" name="penalty_flag2" {{ $filters['customer_id'] == '0' ? 'disabled' : '' }}/>
        </td>
        <td>{{ $penalty->penalty_date }}</td>
        <td>{{ $penalty->chasis_no }}</td>
        <td style="text-align: right;">{{ $penalty->penalty_amount ? number_format($penalty->penalty_amount, 0) : 0; }}</td>
    </tr>
    @endforeach
    </tbody>
</table>
@endif

@if ($cars->count() or $penalties1->count() or $penalties2->count())
<button class="btn btn-large btn-success" onclick="billCreateStep2()" {{ $filters['customer_id'] == '0' ? 'disabled' : '' }}><i class="icon-plus icon-white"></i> Үргэлжлүүлэх</button>
@else
Хайлтад тохирох үр дүн алга
@endif
@stop


@section('pagestyles')
<link rel="stylesheet" type="text/css" href={{ asset('assets/chosen-bootstrap/chosen.min.css') }} />
<link rel="stylesheet" type="text/css" href={{ asset('assets/bootstrap-daterangepicker/daterangepicker.css') }} />
<link rel="stylesheet" type="text/css" href={{ asset('assets/lightbox/lightbox.css') }} />
<style type="text/css" media="screen">
    .container-wrapper {
        /* Attach fixed-th-table to this container,
           in order to layout fixed-th-table
           in the same way as scolled-td-table" */
        position: relative;

        /* Truncate fixed-th-table */
        overflow: hidden;
        width: 1170px;
        white-space: nowrap;
    }
    table thead td {
        font-weight: bold;
    }
    table tbody th {
        font-weight: normal !important;
    }

    .fixed-th-table-wrapper td,
    .fixed-th-table-wrapper th,
    .scrolled-td-table-wrapper td,
    .scrolled-td-table-wrapper th {
        /* Set background to non-transparent color
           because two tables are one above another.
         */
        background: white;
    }
    .fixed-th-table-wrapper {
        /* Make table out of flow */
        position: absolute;
    }
    .fixed-th-table-wrapper th {
        /* Place fixed-th-table th-cells above
           scrolled-td-table td-cells.
         */
        position: relative;
        z-index: 1;
    }
    .scrolled-td-table-wrapper td {
        /* Place scrolled-td-table td-cells
           above fixed-th-table.
         */
        position: relative;
    }
    .scrolled-td-table-wrapper {
        /* Make horizonal scrollbar if needed */
        overflow-x: scroll;
    }

</style>
@stop

@section('pagescripts')
<!--page specific plugin scripts-->
<script type="text/javascript" src={{ asset('assets/chosen-bootstrap/chosen.jquery.min.js') }}></script>
<script type="text/javascript" src={{ asset('assets/bootstrap-daterangepicker/date.js') }} ></script>
<script type="text/javascript" src={{ asset('assets/bootstrap-daterangepicker/daterangepicker.js') }}></script>
<script type="text/javascript" src={{ asset('assets/lightbox/lightbox-2.6.min.js') }}></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('input[name="purchase_date"]').daterangepicker({
            format:"yyyy/MM/dd"
        });
    });
    function setAllBillFlag(bEnable) {
        $('.bill_chbox').prop("checked", bEnable);
    }
    function setAllPenaltyFlag1(bEnable) {
        $('.penalty_chbox1').prop("checked", bEnable);
    }
    function setAllPenaltyFlag2(bEnable) {
        $('.penalty_chbox2').prop("checked", bEnable);
    }

    function billCreateStep2() {
        var customerId = "{{ $filters['customer_id'] }}";
        var cars = new Array();
        cars = $('input:checkbox:checked.bill_chbox').map(function () {
            return (this.id).substring(5);
        }).get();
        var penalties1 = new Array();
        penalties1 = $('input:checkbox:checked.penalty_chbox1').map(function () {
            return (this.id).substring(9);
        }).get();
        var penalties2 = new Array();
        penalties2 = $('input:checkbox:checked.penalty_chbox2').map(function () {
            return (this.id).substring(9);
        }).get();
        if ((cars.length == 0) && (penalties1.length == 0) && (penalties2.length == 0)) {
            // the array is defined and has at least one element
            alert('Та ядаж нэг машин эсвэл penalty сонгоно уу?');
        } else {
            console.log(customerId, cars, penalties1, penalties2);

            var request = $.ajax({
                url: '{{ route('car_balance.bill.create.step') }}',
                type: "get",
                data: { customer : customerId, cars: cars, penalties1: penalties1, penalties2: penalties2},
                dataType: "JSON"
            });

            request.done(function(result) {
                window.location.href = '{{ URL::route('car_balance.bill.create.step2') }}';
                console.log(result.status , result.url);
            });
        }
    }

</script>
@stop